﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using WebApiOrm.Provider;

namespace WebApiOrm.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class OrmsController : ControllerBase
    {
        private readonly IOrmFactory ormFactory;
        private readonly IOptions<OrmFactoryOptions> options;

        public OrmsController(
            IOrmFactory ormFactory,
            IOptions<OrmFactoryOptions> options)
        {
            this.ormFactory = ormFactory;
            this.options = options;
        }

        /// <summary>
        /// 获取注册的orm集合
        /// </summary> 
        /// <returns></returns>
        [HttpGet]
        public Dictionary<string, Version?> Get()
        {
            var result = new Dictionary<string, Version?>();
            foreach (var ormName in this.options.Value.Names)
            {
                var provider = this.ormFactory.Create(ormName);
                result[ormName] = provider.Version;
            }
            return result;
        }
    }
}
